package com.ml.doctor.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.reflect.TypeToken;
import com.ml.doctor.R;
import com.ml.doctor.activity.PatientDetailActivity;
import com.ml.doctor.bean.JianCeBean;
import com.ml.doctor.network.NetworkApi;
import com.ml.doctor.network.NetworkManager;
import com.ml.doctor.utils.LocalShared;
import com.ml.doctor.utils.ToastUtil;
import com.ml.doctor.view.CycleImageView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MonitorFragment extends BaseRefreshLoadFragment {
    List<JianCeBean> list;
    JianCeAdapter jianCeAdapter;
    int page = 1;


    @Override
    protected void initView() {
        super.initView();
        list = new ArrayList<>();
        jianCeAdapter = new JianCeAdapter();
        swipeTarget.setAdapter(jianCeAdapter);
        swipeToLoadLayout.setRefreshing(true);
        swipeTarget.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent(getActivity(), PatientDetailActivity.class);
                i.putExtra("patient", list.get(position).getUser());
                startActivity(i);
            }
        });
    }

    @Override
    public void onLoadMore() {
        super.onLoadMore();
        getData();
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        page = 1;
        getData();
    }

    public void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("docterid", LocalShared.getInstance(getActivity()).getUserId() + "");
        map.put("page", page + "");
        map.put("pagesize", 20 + "");
        map.put("state", 2 + "");
        NetworkManager.getInstance().getResultClass(NetworkApi.BasicUrl + "bl/SelOneDocterAllDate", map, new TypeToken<List<JianCeBean>>() {
        }.getType(), new NetworkManager.SuccessCallback<List<JianCeBean>>() {
            @Override
            public void onSuccess(List<JianCeBean> response) {
                if (page == 1) {
                    list.clear();
                }
                list.addAll(response);
                page++;
                swipeToLoadLayout.setRefreshing(false);
                swipeToLoadLayout.setLoadingMore(false);
                jianCeAdapter.notifyDataSetChanged();
            }
        }, new NetworkManager.FailedCallback() {
            @Override
            public void onFailed(String message) {
                ToastUtil.showShort(getActivity(), message);
                swipeToLoadLayout.setRefreshing(false);
                swipeToLoadLayout.setLoadingMore(false);

            }
        });
    }

    public void changeStatus(String zid) {
        Map<String, String> map = new HashMap<>();
        map.put("docterid", LocalShared.getInstance(getActivity()).getUserId() + "");
        map.put("zid", zid + "");
        NetworkManager.getInstance().getResultClass(NetworkApi.BasicUrl + "/bl/deal_wait", map, new TypeToken<List<JianCeBean>>() {
        }.getType(), new NetworkManager.SuccessCallback<List<JianCeBean>>() {
            @Override
            public void onSuccess(List<JianCeBean> response) {
                swipeToLoadLayout.setRefreshing(true);
            }
        }, new NetworkManager.FailedCallback() {
            @Override
            public void onFailed(String message) {
                ToastUtil.showShort(getActivity(), message);
            }
        });
    }
    class JianCeAdapter extends BaseAdapter {

        ViewHolder viewHolder;

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.item_jiance, parent, false);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }
            viewHolder = (ViewHolder) convertView.getTag();
            Glide.with(getActivity()).load(list.get(position).getUser().user_photo).asBitmap().into(viewHolder.ivAvatar);
            viewHolder.txtName.setText(list.get(position).getBname());
            viewHolder.txtTime.setText("体检时间：" + list.get(position).getTime());
            String result = "";
            if (list.get(position).getTemper_ature() != 0)
                result = result + "体温：" + list.get(position).getTemper_ature() + "℃   ";
            if (list.get(position).getHeart_rate() != 0)
                result = result + "心率：" + list.get(position).getHeart_rate() + "   ";
            if (list.get(position).getPulse() != 0)
                result = result + "脉搏：" + list.get(position).getPulse() + "   ";
            if (list.get(position).getBlood_sugar() != 0)
                result = result + "血糖：" + list.get(position).getBlood_sugar() + "   ";
            if (list.get(position).getBlood_oxygen() != 0)
                result = result + "血氧：" + list.get(position).getBlood_oxygen() + "   ";
            if (list.get(position).getLow_pressure() != 0)
                result = result + "血压：" + list.get(position).getLow_pressure() + "-" + list.get(position).getHigh_pressure() + "   ";
            if (list.get(position).getCholesterol() != 0)
                result = result + "胆固醇：" + list.get(position).getCholesterol() + "   ";
            if (list.get(position).getUric_acid() != 0)
                result = result + "血尿酸：" + list.get(position).getUric_acid() + "   ";
            viewHolder.txtResult.setText(result);

            viewHolder.txtStatus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    changeStatus(list.get(position).getZid() + "");
                }
            });
            return convertView;
        }


        class ViewHolder {
            @BindView(R.id.iv_avatar)
            CycleImageView ivAvatar;
            @BindView(R.id.txt_name)
            TextView txtName;
            @BindView(R.id.txt_time)
            TextView txtTime;
            @BindView(R.id.txt_status)
            TextView txtStatus;

            @BindView(R.id.txt_result)
            TextView txtResult;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }

}
